<?php
	// this is control class , and it will be inited after runing.
	class control
	{
		
		var $get = array();
		public function control()
		{
			$this->load("auth");
			$this->request_init();
		}
		
		function load($model, $base = NULL)
		{
			$base = $base ? $base : $this;
			if(empty($_ENV[$model])) {
				eval('$_ENV[$model] = new '.$model.'($base);');
			}
			return $_ENV[$model];
		}
		
		function request_init()
		{
			// $this->get = explode("&",urldecode($_SERVER['QUERY_STRING']));
			// $this->get = explode("-",$this->get[0]);
			// if(empty($this->get[0]))
				// $this->get[0]='fm';
			// if(empty($this->get[1]))
				// $this->get[1]='default';
			
			// if(!empty($_GET['filename']))
			// {
				// $this->$get = array("remote","upload");
			// }
			$this->run();
			
		}
		
		public function run()
		{
			
			$mod = $_REQUEST['m'];//$this->get[0];//$control = new control($this->get,$this->post);
			$act = $_REQUEST['a'];//$this->get[1];
			//$para = $this->get[2];
			if(class_exists($mod))
			{
				$this->load($mod);
				
				if(method_exists($mod, "do".$act)) {
					
					
					if($this->requirePower($_ENV[$mod],$act))
					{
						if($_ENV["auth"]->haslogin()!=true)
						{
							header("location:"."?user-login&rurl=".urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']));
							exit();
						}
					}

					$act = "do".$act;
					$_ENV[$mod]->$act();
				}else {
					$this->notfound('action "'.$act.'" not found!');
				}
			}else
				$this->notfound('class "'.$mod.'" not found!');
		
		}
		
		function notfound($error){
			@header('HTTP/1.0 404 Not Found');
			exit("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1><p> $error </p></body></html>");
		}
	
		function requirePower($c,$m)
		{
			return (!empty($c->checkMethod))?in_array($m,$c->checkMethod):false;
		}
	}
?>